package com.choosefine.springboot.devops.base.common.bean;

import org.slf4j.LoggerFactory;
import org.springframework.web.util.DefaultUriTemplateHandler;
import org.springframework.web.util.UriTemplateHandler;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by 邱鹏 on 2016/7/20.
 */
public class SimpleTemplateBean {
    public enum TemplateType {DOLLAR, JSP_SCRIPT}

    UriTemplateHandler uriTemplateHandler = new DefaultUriTemplateHandler();
    org.slf4j.Logger logger = LoggerFactory.getLogger("api service v1");

    /**
     * @param src
     * @param data
     * @param type
     * @return
     */
    public String template(String src, Map<String, Object> data, TemplateType type) {
        String result = null;
        switch (type) {
            case DOLLAR:
                result = dollarBigBracket(src, data);
                break;
            case JSP_SCRIPT:
                result = jspScriptTpl(src, data);
                break;
            default:
        }
        return result;
    }

    /**
     * 替换模板变量
     * 模板样式为：${xxx}
     *
     * @param template
     * @param data
     * @return
     */
    private String dollarBigBracket(String template, Map<String, Object> data) {
        StringBuffer sb = new StringBuffer();
        try {
            Pattern pattern = Pattern.compile("\\$\\{(.+?)\\}");
            Matcher matcher = pattern.matcher(template);
            while (matcher.find()) {
                String name = matcher.group(1);// 键名
                if(data.containsKey(name)) {
                    String value = data.get(name)+"";// 键值
                    if (value == null) {
                        value = "";
                    } else {
                        value = value.replaceAll("\\$", "\\\\\\$");
                    }
                    matcher.appendReplacement(sb, value);
                }else{
                    logger.error("模版缺少参数：\n"+name);
                }
            }
            matcher.appendTail(sb);
        } catch (Exception e) {
            logger.error("无法编译通过的模版内容是：\n"+template);
            e.printStackTrace();
        }
        return sb.toString();
    }
    /**
     * 替换模板变量
     * 模板样式为：${xxx}
     *
     * @param template
     * @return
     */
    private List dollarBigBracketParams(String template) {
        List result=new ArrayList();
        try {
            Pattern pattern = Pattern.compile("\\$\\{(.+?)\\}");
            Matcher matcher = pattern.matcher(template);
            while (matcher.find()) {
                String name = matcher.group(1);// 键名
                result.add(name);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    /**
     * @param src
     * @param data
     * @return
     */
    public String jspScriptTpl(String src, Map<String, Object> data) {
        for (String key : data.keySet()) {
            src = src.replaceAll("<%" + key + "%>", data.get(key) + "");
        }
        return src;
    }

    public String urlTemplate(String url, Map map) {
        return uriTemplateHandler.expand(url, map) + "";
    }

    public String urlTemplate(String url, Object... urlVars) {
        return uriTemplateHandler.expand(url, urlVars) + "";
    }

}
